package com.example.yuyin;


import com.example.db.DatabaseHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class FtpSetting extends Activity {

	private EditText ftp_hostname;  
	private EditText ftp_port;  
	private EditText ftp_username;  
	private EditText ftp_password;  
	private String HostName = null;  
	private String Port = null;  
	private String UserName = null;  
	private String Password = null;  
	private Button SaveBTN;  
	private Button CancelBTN;  

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ftpsetting);

		SaveBTN = (Button)findViewById(R.id.save);
		SaveBTN.setOnClickListener(SaveBtnListener);

		CancelBTN = (Button)findViewById(R.id.cancel);
		CancelBTN.setOnClickListener(CancelBtnListener);

		ftp_hostname = (EditText)findViewById(R.id.edittext_hostname);
		ftp_port = (EditText)findViewById(R.id.edittext_port);
		ftp_username = (EditText)findViewById(R.id.edittext_username);
		ftp_password = (EditText)findViewById(R.id.edittext_password);
		
		GetFtpParam();
	}

	public void  UpdateFtpParam() {
		DatabaseHelper dbHelper = new DatabaseHelper(FtpSetting.this,"YuYin");
		String StrSQL = "delete from FtpParam;;";
		dbHelper.delete(StrSQL);

		StrSQL = "INSERT INTO FtpParam (HostName, Port,UserName,Password) VALUES (\""+
				ftp_hostname.getText().toString() +"\",\""+
				ftp_port.getText().toString() +"\",\""+
				ftp_username.getText().toString() +"\",\""+
				ftp_password.getText().toString() +"\");";
		
		dbHelper.insert(StrSQL);
		dbHelper.close();
	}

	public void GetFtpParam() {
		DatabaseHelper dbHelper = new DatabaseHelper(FtpSetting.this,"YuYin");
		String StrSQL = "SELECT  HostName, Port ,UserName,Password FROM FtpParam";
		Cursor cursor = dbHelper.select(StrSQL);
		while(cursor.moveToNext()){
			HostName = cursor.getString(cursor.getColumnIndex("HostName"));
			Port = cursor.getString(cursor.getColumnIndex("Port"));
			UserName = cursor.getString(cursor.getColumnIndex("UserName"));
			Password = cursor.getString(cursor.getColumnIndex("Password"));
		}
		cursor.close();
		dbHelper.close();

		if (HostName != null || Port != null || UserName != null || Password != null)
		{
			ftp_hostname.setText(HostName);
			ftp_port.setText(Port);
			ftp_username.setText(UserName);
			ftp_password.setText(Password);
		}
	}

	OnClickListener SaveBtnListener = new OnClickListener()
	{
		public void onClick(View v) 
		{
			if (!ftp_hostname.getText().toString().equals("") && 
					!ftp_port.getText().toString().equals("")&&
				!ftp_username.getText().toString().equals("") &&
				!ftp_password.getText().toString().equals(""))
			{
				UpdateFtpParam();
				finish();
			}
			else {
				Dialog dialog = new AlertDialog.Builder(FtpSetting.this).setTitle("警告").setMessage("您输入的信息不全！").setPositiveButton("确定", new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int which)
					{
					}
				}).create();
				dialog.show();
			}
			
		}  
	};  

	OnClickListener CancelBtnListener = new OnClickListener()
	{  
		public void onClick(View v) 
		{  
			finish();
		}  
	};  


}
